package com.huazi.www.frontdesk.dao.impl;

import com.huazi.www.frontdesk.dao.FrontdeskCardDao;
import com.huazi.www.frontdesk.javaBase.ConsumeInfo;
import com.huazi.www.frontdesk.javaBase.DepartmentInfo;
import com.huazi.www.frontdesk.javaBase.FrontdeskCardInfo;
import com.huazi.www.frontdesk.javaBase.QueueInfo;
import com.huazi.www.frontdesk.util.BaseDao;


import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 业务实现类
 */
public class FrontdeskCardDaoImpl extends BaseDao implements FrontdeskCardDao {
    /**
     * 用户注册
     *
     * @param password//密码
     * @param idCard//身份证号码
     * @param name//姓名
     * @param sex//性别
     * @param phone//电话
     * @param age//年龄
     * @return
     */
    @Override
    public Integer register(String password, String idCard, String name, String sex, String phone, Integer age) {
        String sql = "INSERT INTO frontdesk_card (`password`,idCard,`name`,sex,phone,age) VALUES (?,?,?,?,?,?)";//插入密码，身份证号码，姓名，性别，电话，年龄


        return executeUpdate(sql, password, idCard, name, sex, phone, age);
    }

    /**
     * 分页查询挂号表
     *
     * @param patientId//用户编号
     * @param star//起始下标
     * @param pageSize//查询条数
     * @return
     */
    @Override
    public List<QueueInfo> pagequeue(Integer patientId, Integer star, Integer pageSize) {
        ResultSet resultSet = null;
        List<QueueInfo> list = new ArrayList<>();
        String sql = "select id,patientId,patientName,doctorid,time,state from doctor_queue ";//查询id，用户编号,用户名，医生编号，看诊时间，状态
        if (patientId != null && patientId != 0) {
            sql += " where patientId=? limit  ?,?";//id不为空时加条件
            resultSet = executeQuery(sql, patientId, star, pageSize);
        } else {
            sql += " limit  ?,?";
            resultSet = executeQuery(sql, star, pageSize);
        }
        if (resultSet != null) {
            try {
                while (resultSet.next()) {
                    Integer id = resultSet.getInt(1);
                    Integer patientIds = resultSet.getInt(2);
                    String patientName = resultSet.getString(3);
                    Integer doctorid = resultSet.getInt(4);
                    Date time = resultSet.getDate(5);
                    Integer state = resultSet.getInt(6);
                    QueueInfo queueInfo = new QueueInfo(id, patientIds, patientName, doctorid, time, state);
                    list.add(queueInfo);

                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                closeShow(null, null, resultSet);
            }
        }
        return list;
    }

    /**
     * 分页查询挂号表的总记录数
     *
     * @param patientId//用户编号
     * @return
     */
    @Override
    public Integer count(Integer patientId) {
        ResultSet resultSet = null;
        Integer i = 0;
        String sql = "select count(*) from doctor_queue";
        if (patientId != null && patientId != 0) {
            sql += " where patientId=?";
            resultSet = executeQuery(sql, patientId);
        } else {
            resultSet = executeQuery(sql);
        }
        if (resultSet != null) {
            try {
                while (resultSet.next()) {
                    i = resultSet.getInt(1);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                closeShow(null, null, resultSet);
            }
        }
        return i;
    }

    /**
     * 转诊
     *
     * @param xiangxi//科室
     * @param patientId//用户编号
     * @return
     */
    @Override
    public Integer transfer(String xiangxi, Integer patientId) {
        String sql = "UPDATE `doctor_queue` SET `doctorid`=(SELECT `doctorid` FROM `user_department` WHERE `xiangxi`=?)  WHERE `patientId`=?";//根据科室查询更新值
        return executeUpdate(sql, xiangxi, patientId);
    }

    /**
     * 查询科室
     *
     * @return
     */
    @Override
    public List<DepartmentInfo> list() {
        String sql = "SELECT id,xiangxi,doctorid FROM `user_department`";//查询id,科室，医生编号
        ResultSet rs = executeQuery(sql);
        List<DepartmentInfo> list = new ArrayList<>();
        try {
            while (rs.next()) {
                DepartmentInfo d = new DepartmentInfo();
                d.setId(rs.getInt(1));
                d.setDtname(rs.getString(2));
                d.setDoctorid(rs.getInt(3));
                list.add(d);


            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    /**
     * 分页查询用户表
     *
     * @param id//用户编号
     * @param star//起始下标
     * @param pageSize//查询条数
     * @return
     */
    @Override
    public List<FrontdeskCardInfo> pagefrontdeskcard(Integer id, Integer star, Integer pageSize) {
        ResultSet resultSet = null;
        List<FrontdeskCardInfo> list = new ArrayList<>();
        String sql = "select id,idCard,name,sex,phone,age from frontdesk_card";//查询id，身份证，姓名，性别，电话，年龄
        if (id != null && id != 0) {
            sql += " WHERE `id`=? limit  ?,?";
            resultSet = executeQuery(sql, id, star, pageSize);
        } else {
            sql += " limit  ?,?";
            resultSet = executeQuery(sql, star, pageSize);
        }
        if (resultSet != null) {
            try {
                while (resultSet.next()) {
                    Integer ids = resultSet.getInt(1);
                    String idCard = resultSet.getString(2);
                    String name = resultSet.getString(3);
                    String sex = resultSet.getString(4);
                    String phone = resultSet.getString(5);
                    Integer age = resultSet.getInt(6);
                    FrontdeskCardInfo frontdeskCardInfo = new FrontdeskCardInfo(ids, idCard, name, sex, phone, age);
                    list.add(frontdeskCardInfo);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                closeShow(null, null, resultSet);
            }
        }
        return list;
    }

    /**
     * 分页查询用户表的总记录数
     *
     * @param id//用户编号
     * @return
     */
    @Override
    public Integer countfrontdeskcard(Integer id) {
        ResultSet resultSet = null;
        Integer i = 0;
        String sql = "select count(*) from frontdesk_card";
        if (id != null && id != 0) {
            sql += " where `id`=?";
            resultSet = executeQuery(sql, id);
        } else {
            resultSet = executeQuery(sql);
        }
        if (resultSet != null) {
            try {
                while (resultSet.next()) {
                    i = resultSet.getInt(1);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                closeShow(null, null, resultSet);
            }
        }
        return i;
    }

    /**
     * 根据id查询用户表用户信息
     *
     * @param id//用户编号
     * @return
     */
    @Override
    public List<FrontdeskCardInfo> idqueue(Integer id) {
        ResultSet resultSet = null;
        List<FrontdeskCardInfo> list = new ArrayList<>();
        String sql = "select id,idCard,`name`,sex,phone,age,money from frontdesk_card where id=?";//根据id查询用户id，身份证，姓名，性别，电话，余额
        resultSet = executeQuery(sql, id);
        if (resultSet != null) {
            try {
                while (resultSet.next()) {
                    Integer ids = resultSet.getInt(1);
                    String idCard = resultSet.getString(2);
                    String name = resultSet.getString(3);
                    String sex = resultSet.getString(4);
                    String phone = resultSet.getString(5);
                    Integer age = resultSet.getInt(6);
                    Double money = resultSet.getDouble(7);
                    FrontdeskCardInfo frontdeskCardInfo = new FrontdeskCardInfo(ids, idCard, name, sex, phone, age, money);
                    list.add(frontdeskCardInfo);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                closeShow(null, null, resultSet);
            }
        }
        return list;
    }

    /**
     * 根据id删除用户表用户信息
     *
     * @param id//用户编号
     * @return
     */
    @Override
    public Integer frontdeskcardupdate(Integer id) {
        String sql = "delete from frontdesk_card where id=?";
        return executeUpdate(sql, id);
    }

    /**
     * 补卡
     *
     * @param password//密码
     * @param idCard//身份证号码
     * @param name//姓名
     * @param sex//性别
     * @param phone//电话号码
     * @param age//年龄
     * @param money//余额
     * @return
     */
    @Override
    public Integer reissueregister(String password, String idCard, String name, String sex, String phone, Integer age, Double money) {
        String sql = "INSERT INTO frontdesk_card (`password`,idCard,`name`,sex,phone,age,money) VALUES (?,?,?,?,?,?,?)";
        return executeUpdate(sql, password, idCard, name, sex, phone, age, money);
    }

    /**
     * 分页查询缴费记录表
     *
     * @param name//按名字模糊查询
     * @param star//起始下标
     * @param pageSize//查询条数
     * @return
     */
    @Override
    public List<ConsumeInfo> pageconsume(String name, Integer star, Integer pageSize) {
        List<ConsumeInfo> list = new ArrayList<>();
        String sql = "select f.`name`,c.consumeType,c.drugType,c.times,c.money from consumeinfo c,frontdesk_card f where c.cardNo=f.id and f.`name` like CONCAT('%',?,'%') limit  ?,?";
        ResultSet resultSet = executeQuery(sql, name, star, pageSize);
        if (resultSet != null) {
            try {
                while (resultSet.next()) {
                    String names = resultSet.getString(1);
                    String consumeType = resultSet.getString(2);
                    String drugType = resultSet.getString(3);
                    Date times = resultSet.getDate(4);
                    double money = resultSet.getDouble(5);
                    ConsumeInfo consumeInfo = new ConsumeInfo(names, consumeType, drugType, times, money);
                    list.add(consumeInfo);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                closeShow(conn, pstmt, resultSet);
            }
        }
        return list;
    }

    /**
     * //分页查询缴费记录表的总记录数
     *
     * @param name//按名字模糊查询
     * @return
     */
    @Override
    public Integer consumecount(String name) {
        ResultSet resultSet = null;
        Integer i = 0;
        String sql = "select count(*) from consumeinfo c,frontdesk_card f where c.cardNo=f.id";
        if (name != null && name != "") {
            sql += " and f.`name` like CONCAT('%','?','%')";
            resultSet = executeQuery(sql, name);
        } else {
            resultSet = executeQuery(sql);
        }
        if (resultSet != null) {
            try {
                while (resultSet.next()) {
                    i = resultSet.getInt(1);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                closeShow(null, null, resultSet);
            }
        }
        return i;
    }

    /**
     * 充值
     *
     * @param //用户id
     * @param money//充值金额
     * @return
     */
    @Override
    public Integer recharging(Integer id, Double money) {
        String sql = "update frontdesk_card set money=money+? where id=?";
        int i = executeUpdate(sql, money, id);
        return i;

    }

    /**
     * 返回刚注册用户的id
     *
     * @param idCard//身份证号码
     * @return
     */
    @Override
    public Integer id(String idCard) {
        String sql = "select id from frontdesk_card where idCard=?";
        ResultSet resultSet = executeQuery(sql, idCard);
        Integer i = null;
        if (resultSet != null) {
            try {
                while (resultSet.next()) {
                    i = resultSet.getInt(1);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return i;
    }

}
